package com.onlineauction.beans;

import java.util.ArrayList;
import java.util.List;

import com.onlineauction.entities.Auction;
import com.onlineauction.entities.Bid;
import com.onlineauction.entities.Item;
import com.onlineauction.entities.User;
import com.onlineauction.helpers.Constants.Category;

public class MakeBidBean {

	private String username;
	private User user;
	private String itemNameForSearch;
	private Category category;
	private String categoryName;
	private ArrayList<MakeBidDataRow> dataRows;

	public static class MakeBidDataRow {
		public long itemID;
		public String itemName;
		public String categoryName;
		public String username;
		public String basePrice;
		public String bestPrice;
		public String auctionOpen;
		public Auction auction;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public User getUser() {
		if (user == null) {
			this.user = new User(username);
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getItemNameForSearch() {
		return itemNameForSearch;
	}

	public void setItemNameForSearch(String itemNameForSearch) {
		if (itemNameForSearch == null || itemNameForSearch.length() == 0) {
			itemNameForSearch = "";
		}
		this.itemNameForSearch = itemNameForSearch;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
		try {
			this.category = Category.valueOf(categoryName);
		} catch (Exception e) {
			this.category = null;
		}
	}

	private void doSearch() {
		List<Item> items = null;
		if (this.itemNameForSearch == null || this.itemNameForSearch.length() == 0) {
			if (this.category == null) {
				items = Item.searchForAllItems();
			} else {
				items = Item.searchItemsByCategory(this.category);
			}
		} else {
			if (this.category == null) {
				items = Item.searchItemsByProductName(this.itemNameForSearch);
			} else {
				items = Item.searchItemsByProductNameInCategory(this.itemNameForSearch,
						this.category);
			}
		}

		this.dataRows = new ArrayList<MakeBidDataRow>();
		for (Item item : items) {
			MakeBidDataRow row = new MakeBidDataRow();
			row.itemID = item.getItemID();
			row.itemName = item.getProductName();
			row.categoryName = item.getCategory().toString();
			row.username = item.getOwner();
			
			row.auctionOpen = "Closed";
			row.basePrice = "";
			row.bestPrice = "";
			List<Auction> auctions = Auction.getLiveAuctionsOfItem(item.getItemID());
			if (auctions != null && auctions.size() > 0) {
				row.auctionOpen = "Open";				

				Auction auction = auctions.get(0);
				row.auction = auction;
				
				row.basePrice = String.format("CAD$ %.2f", auction.getAskPrice());

				Long bestBidID = Bid.getBestBidIDOfAuction(auction.getAuctionID());
				if (bestBidID != null) {
					row.bestPrice = String.format("CAD$ %.2f", new Bid(bestBidID).getBidPrice());
				} 
			}
			
			this.dataRows.add(row);
		}

	}

	public ArrayList<MakeBidDataRow> getDataRows() {
		this.doSearch();
		return dataRows;
	}

	public void setDataRows(ArrayList<MakeBidDataRow> dataRows) {
		this.dataRows = dataRows;
	}

}
